from django.http import JsonResponse
from django.shortcuts import render
from booktest.models import BookInfo,HeroInfo

from django.db.models import F,Q,Sum

# Create your views here.

 # 新增
# book = BookInfo()
# book.btitle = '三国'
# book.bpub_date = '1995-11-11'
# book.save()
#
# book2 = BookInfo(
#     btitle='小三国',
#     bpub_date='1992-11-11'
# )
# book2.save()
#
# # 如果创建模型对象时,直接调用create方法会创建模型并保存到数据库的表中
# book3 = BookInfo.objects.create(
#     btitle='西游记',
#     bpub_date='1998-11-11'
# )
#
# hero = HeroInfo(
#     hname='孙悟空',
#     # hbook=book3,  # 外键的关联赋值
#     hbook_id=book3.id
#
# )


"""以下是基本查询"""
# get/  count  /all

# BookInfo.objects.get(id=1)
# BookInfo.objects.get(btitle='西游记')
#
# BookInfo.objects.all()
# BookInfo.objects.count()

# BookInfo.objects.filter(id=1).count()


"""以下是过滤查询"""
# BookInfo.objects.filter(btitle__contains='x')
# BookInfo.objects.filter(id__exact=1)
# BookInfo.objects.filter(id=1)
#
# BookInfo.objects.filter(btitle__contains='湖')
#
# BookInfo.objects.filter(btitle__endswith='部')

#
#
# BookInfo.objects.filter(btitle__isnull=False)
#
# BookInfo.objects.filter(id__in=[2, 4])  # id__in不是指定范围,而是指定的具体那几个
#
# BookInfo.objects.filter(id__gt=2)

# BookInfo.objects.exclude(id=3)
#
# BookInfo.objects.filter(bpub_date__year=1980)
#
# BookInfo.objects.filter(bpub_date__gt='1990-1-1')

# 如果想要进行两个字段进行比较 需要 F对象
# BookInfo.objects.filter(bread__gt=F('bcomment'))
# BookInfo.objects.filter(bread__gt=F('bcomment') * 2)

# 以下演示Q对象  id小于等于3 并且bread 要大于30  # 逻辑与
# BookInfo.objects.filter(id__lte=3, bread__gt=30)

# BookInfo.objects.filter(Q(id__lte=3) & Q(bread__gt=30))  # 逻辑与, 同时满足多个条件的才要

# BookInfo.objects.filter(Q(bread__gt=20) | Q(pk__lt=3))  # 逻辑或, 只要满足其中一个条件的数据都要
#
# BookInfo.objects.filter(~Q(id=3))

# 以下聚合函数
# BookInfo.objects.aggregate(Sum('bread'))
# BookInfo.objects.aggregate(Avg('bread'))


# BookInfo.objects.all().order_by('bread')  # 默认升序
# BookInfo.objects.all().order_by('-bread')  # 默认升序

"""
在定义模型时,如果两个模型之间有关键关键,一般都会把外键定义在多的一方  hbook
那么在一的这方会隐式生成一个字段  heroinfo_set
"""
# 一查多  : 先把一查询出来.然后再用一的这方模型对象.多方模型对象名小写_set属性
# book = BookInfo.objects.get(id=1)
# book.heroinfo_set.all()
# book.heroinfo_set.get(hname='郭靖')

# 多查一:    先把多查询出来,然后再用多的这方模型对象.外键就可以查到对应一这方
# hero = HeroInfo.objects.get(hname='郭靖')
# hero.hbook


"""关联过滤查询"""
# 一查多:  最终想要那个的数据就以那个模型类名开头.objects.filter(外键或多的方模型名小写__对应方的属性)
# 多的模型名.objects.filter(外键__一这方属性=值)
# HeroInfo.objects.filter(hbook__id=2)

# 多查一
# 一的模型名.objects.filter(多的这方模型名小写__多的这方模型中属性=值)
# BookInfo.objects.filter(heroinfo__hname='孙悟空')



"""数据修改"""
# 先把要修改的数据查询出来,然后修改相应属性的值,然后再调用模型对象的save()
# book = BookInfo.objects.get(btitle='三国')
# book.btitle = '大大三国'
# book.save()


# filter().update()  查询的同时直接修改
# BookInfo.objects.filter(btitle='大大三国').update(btitle='小小三国')


"""数据删除"""
# delete()
# book = BookInfo.objects.get(id=6)
# book.delete()

# BookInfo.objects.filter(id__in=[5, 7]).delete()


# def book_info(request):
#     books = BookInfo.objects.all()
#
#     book_list = []
#     for book in books:
#         book_list.append(book.btitle)
#     return JsonResponse({"book_list":book_list},json_dumps_params={'ensure_ascii':False})


